//
//  LBMeController.m
//  loveBeen
//
//  Created by lc on 2016/11/20.
//  Copyright © 2016年 loveBeen. All rights reserved.
//

#import "LBMeController.h"
#import "LBMeMainModel.h"
#import "LBMeThreeBtn.h"
#import "LBOrderController.h"
#import "LBMeFeedBackController.h"
#import "LBCouponController.h"
#import "LBMeMessageSystemViewController.h"
#import "LBMeShippingAddrController.h"
#define WIDTH  [UIScreen mainScreen].bounds.size.width
static NSString *ID = @"cell_id";


@interface LBMeController ()<UITableViewDelegate,UITableViewDataSource,LBMeThreeBtnDelegate>

@property(nonatomic,strong)NSArray *listArrayModel;
@property(nonatomic,strong)NSMutableArray<LBMeMainModel *>*arryM;
@property(nonatomic,weak)UIView *topView;
@end

@implementation LBMeController

- (void)viewDidLoad {
    
    [super viewDidLoad];
   // self.view.backgroundColor = [UIColor brownColor];
    //self.edgesForExtendedLayout = UIRectEdgeNone;
    [self loadData];
    [self setupUI];
}

-(void)loadData {
    
    //模型转换
    NSMutableArray *outArrayM = [[NSMutableArray alloc] init];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"LBMeMain.plist" withExtension:nil];
    NSArray *array = [NSArray arrayWithContentsOfURL:url];
    
    for (NSArray *arrayD in array) {
        _arryM = [NSMutableArray array];
        
        for (NSDictionary *dict in arrayD) {
            LBMeMainModel *model = [[LBMeMainModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [_arryM addObject:model];
        }
        
        [outArrayM addObject:_arryM];
    }
    
    _listArrayModel = outArrayM.copy;
    
}


-(void)setupUI {
    
    //创建3个button
    LBMeThreeBtn *threeBtn = [[LBMeThreeBtn alloc] init];
    threeBtn.backgroundColor = [UIColor grayColor];
    [self.view addSubview:threeBtn];
    
    //设置代理
    threeBtn.delegate = self;
    
        //创建tableview
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [self.view addSubview: tableView];
    
    
    //约束threeBtn
    [threeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       // make.top.equalTo(self.view);
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(136);
        make.height.mas_equalTo(50);
    }];
    
    //设置自动行高
    
//    tableView.estimatedRowHeight = 80;
//    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.rowHeight = 60;
    
    //设置代理
    tableView.dataSource = self;
    tableView.delegate = self;
    
    //约束tableView
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.bottom.equalTo(self.view);
        make.top.mas_equalTo(200);
    }];
    
    //注册cell
    [tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:ID];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _listArrayModel.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section % 2 == 0) {
        return 2;
    }else {
        return 1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    LBMeMainModel *model = _listArrayModel[indexPath.section][indexPath.row];
    cell.imageView.image =[UIImage imageNamed:model.icon];
    cell.textLabel.text = model.message;
    
    return cell;
}

-(void)LBMeThreeBt:(UIButton *)btn {
    if (btn.tag == kButtonOrder) {
        //跳转到我的订单
        LBOrderController *oderVc = [[LBOrderController alloc] init];
                                     
        [self.navigationController pushViewController:oderVc animated:YES];
    }else if(btn.tag == kButtonCheap){
        //跳转到优惠券
        LBCouponController *couponVc = [[LBCouponController alloc] init];
        [self.navigationController pushViewController:couponVc animated:YES];
    }else if (btn.tag == kButtonMessage){
        //跳转到我的消息
        LBMeMessageSystemViewController *myMessage = [[LBMeMessageSystemViewController alloc]init];
        [self.navigationController pushViewController:myMessage animated:YES];
    }else {
        return;
    }
    
    
}


//改变组间距
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 8;
}


-(void)viewWillDisappear:(BOOL)animated {
    [self.topView removeFromSuperview];
}

-(void)viewWillAppear:(BOOL)animated{
    //获取头部视图
    UIView *topView = [[[UINib nibWithNibName:@"LBMeMainTopView" bundle:nil] instantiateWithOwner:nil options:nil] lastObject];
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:topView];
    
    //[self.navigationController.view addSubview:topView];
    // [self.view addSubview: topView];
    self.topView = topView;
    
    //约束头部视图
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        //xib宽度不能写死
        //make.width.mas_equalTo(WIDTH);
        //make.height.mas_equalTo(122);
        //make.top.left.right.mas_equalTo(self.navigationController.view);
        make.top.left.right.equalTo(window);
    }];

    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
}

//cell被选择
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    if (indexPath.row == 1 && indexPath.section == 2) {
        //进入意见反馈
        LBMeFeedBackController *feedBack = [[LBMeFeedBackController alloc] init];
        [self.navigationController pushViewController:feedBack animated:YES];
    }else if(indexPath.row == 0 && indexPath.section == 0){
        //进入送货地址
        LBMeShippingAddrController *addr = [[LBMeShippingAddrController alloc] init];
        [self.navigationController pushViewController:addr animated:YES];
    }
}


@end
